<template>
	<xn-form-container
		:title="formData.id ? '编辑垫付金额' : '增加垫付金额'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="登记日期：" name="registrationDate">
				<a-date-picker
					v-model:value="formData.registrationDate"
					value-format="YYYY-MM-DD"
					placeholder="请选择创建日期"
					style="width: 150px"
				/>
			</a-form-item>
			<a-form-item label="客户ID：" name="clientId">
				<a-input v-model:value="formData.clientId" placeholder="请输入客户ID" allow-clear />
			</a-form-item>
			<a-form-item label="欠款人姓名：" name="debtorName">
				<a-input v-model:value="formData.debtorName" placeholder="请输入欠款人姓名" allow-clear />
			</a-form-item>
			<a-form-item label="欠款金额：" name="unpaidAmount">
				<a-input v-model:value="formData.unpaidAmount" placeholder="请输入欠款金额" allow-clear />
			</a-form-item>
			<a-form-item label="减免金额：" name="discountAmount">
				<a-input v-model:value="formData.discountAmount" placeholder="请输入减免金额" allow-clear />
			</a-form-item>
			<a-form-item label="甲方需还金额：" name="needRepaymentAmount">
				<a-input v-model:value="formData.needRepaymentAmount" placeholder="请输入甲方需还金额" allow-clear />
			</a-form-item>
			<a-form-item label="实还金额：" name="amountReturned">
				<a-input v-model:value="formData.amountReturned" placeholder="请输入实还金额" allow-clear />
			</a-form-item>
			<a-form-item label="垫付金额：" name="advancedPaymentAmount">
				<a-input v-model:value="formData.advancedPaymentAmount" placeholder="请输入垫付金额" allow-clear />
			</a-form-item>
			<a-form-item label="员工账号：" name="employeeAccount">
				<a-input v-model:value="formData.employeeAccount" placeholder="请输入员工账号" allow-clear />
			</a-form-item>
			<a-form-item label="操作员工：" name="operatingEmployee">
				<a-input v-model:value="formData.operatingEmployee" placeholder="请输入操作员工" allow-clear />
			</a-form-item>
			<a-form-item label="总佣金：" name="totalCommissionEstimate">
				<a-input v-model:value="formData.totalCommissionEstimate" placeholder="请输入总佣金" allow-clear />
			</a-form-item>
			<a-form-item label="实际佣金：" name="actualCommissionEstimate">
				<a-input v-model:value="formData.actualCommissionEstimate" placeholder="请输入实际佣金" allow-clear />
			</a-form-item>
			<a-form-item label="垫付比例：" name="advancePaymentRatio">
				<a-input v-model:value="formData.advancePaymentRatio" placeholder="请输入垫付比例" allow-clear />
			</a-form-item>
			<a-form-item label="佣金比例：" name="commissionRatio">
				<a-input v-model:value="formData.commissionRatio" placeholder="请输入佣金比例" allow-clear />
			</a-form-item>
			<a-form-item label="账龄：" name="accountAge">
				<a-input v-model:value="formData.accountAge" placeholder="请输入账龄" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="advancePaymentsForm">
	import { cloneDeep } from 'lodash-es'
	import monthlyApi from '@/api/bi/goals/monthly/monthlyApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	// 打开抽屉
	const onOpen = (parentId, record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		} else {
			formData.value.gzxMonthlyGoalsId = parentId
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		registrationDate: [{ required: true, message: '请选择登记日期', trigger: 'blur' }],
		clientId: [{ required: true, message: '请输入客户ID', trigger: 'blur' }],
		debtorName: [{ required: true, message: '请输入欠款人姓名', trigger: 'blur' }],
		unpaidAmount: [{ required: true, message: '请输入欠款金额', trigger: 'blur' }],
		discountAmount: [{ required: true, message: '请输入减免金额', trigger: 'blur' }],
		needRepaymentAmount: [{ required: true, message: '请输入甲方需还金额', trigger: 'blur' }],
		advancedPaymentAmount: [{ required: true, message: '请输入垫付金额', trigger: 'blur' }],
		employeeAccount: [{ required: true, message: '请输入员工账号', trigger: 'blur' }],
		operatingEmployee: [{ required: true, message: '请输入操作员工', trigger: 'blur' }],
		totalCommissionEstimate: [{ required: true, message: '请输入总佣金', trigger: 'blur' }],
		actualCommissionEstimate: [{ required: true, message: '请输入实际佣金', trigger: 'blur' }],
		advancePaymentRatio: [{ required: true, message: '请输入垫付比例', trigger: 'blur' }],
		commissionRatio: [{ required: true, message: '请输入佣金比例', trigger: 'blur' }],
		accountAge: [{ required: true, message: '请输入账龄', trigger: 'blur' }],
		amountReturned: [{ required: true, message: '请输入实还金额', trigger: 'blur' }]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				monthlyApi
					.advancePaymentsSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
